export function getUrlParams(variable){
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if(pair[0] == variable){return pair[1];}
  }
  return(false);
}

export function checkPhoneNumber(number) {
  return /^1[3456789]\d{9}$/.test(number)
}

/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

export function formatText(text) {
  return text ? text.replace(/\n/g, '<br>') : '';
}

export function hideLoading() {
  const _dom = document.querySelector('.app-loading')
  _dom.style.opacity = 0;
  setTimeout(function() {
    _dom.parentNode.removeChild(_dom);
  }, 1000)
}


export function checkEmailFormat(email) {
  // 定义正则表达式验证邮箱格式
  var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
 
  // 检查邮箱格式是否符合正则表达式
  return regex.test(email);
}

export function changeSearchParams(newParams) {
  const url = new URL(window.location);
  url.search = new URLSearchParams(newParams).toString();
  window.history.pushState({ path: url.href }, '', url.href);
}